/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package bo2s.DAO;

import bo2s.DAO.ConnectionDB;
import bo2s.data.Article;
import bo2s.data.InforPost;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author dinhxuan239
 */
public class PostDAO {
//    public Vector<InforPost> getInfo(){
//      Vector<InforPost> ip=new Vector<InforPost>(3,1);
//      
//      connection cdata=new ConnectionManager();
//      String sql="SELECT * FROM sprint1.article order by id desc limit 5 ";
//      try {
//          Connection conn= cdata.getConnection();
//          PreparedStatement stmt=conn.prepareCall(sql);
//          ResultSet rs=stmt.executeQuery(sql);
//          while(rs.next())
//          {
//          InforPost inp= new InforPost();
//          inp.setId(rs.getInt("id"));
//          inp.setName(rs.getString("name"));
//          inp.setContent(rs.getString("content"));
//          inp.setCreated(rs.getString("created"));
//          inp.setPublish(rs.getInt("published"));
//          inp.setUserid(rs.getInt("user_id"));
//          ip.add(inp);
//          }
//      } catch (SQLException ex) {
//          Logger.getLogger(bo2s.DAO.PostDAO.class.getName()).log(Level.SEVERE, null, ex);
//      }
//      return ip;
//  }
  Connection connection=null;
 PreparedStatement stmt=null;
 public PostDAO(){
	 connection=ConnectionDB.getConnection();
 }
 public void Delete(String userId)
 {
    try{
	String sql="Delete From Article Where id=?";
	stmt=this.connection.prepareStatement(sql);
	stmt.setString(1, userId);//xóa theo mã được truyền vào 
	stmt.executeUpdate();
	}catch(Exception e){
            e.printStackTrace();
	}
 }
 public List<Article> getListArticle(){
	 List<Article> articles=new ArrayList<Article>(); //list đối tượng Users -.chú ý cách sử dụng loại này
		try{
			 
			 Statement statement = this.connection.createStatement();
			 
	            ResultSet rs = statement.executeQuery("select * from article");
			 
			while(rs.next()){
				Article article=new Article();
				article.setId(rs.getInt("id"));
				article.setName(rs.getString("name"));
				article.setDescription(rs.getString("description"));
				article.setContent(rs.getString("content"));
				article.setPublished(rs.getInt("published"));
				article.setCreated(rs.getString("created"));
				article.setCategory_id(rs.getInt("category_id"));
				article.setUser_id(rs.getInt("user_id")); 
				articles.add(article);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return articles;
 }
 }
